<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 10:12
 */

namespace app\admin\controller;


use app\admin\model\ShopInfo;

class ShopInfoController extends BaseController
{
    public function index()
    {
        if($this->request->isPost()){
            $params = $this->request->param();
            if($this->request->file('img')){
                $file = uploadFile('img',null,null,'uploads/shop_info');
                $params['img'] = $file['origin'];
                $params['img_origin'] = $file['tag'];
                $oldImg = ShopInfo::field('img,img_origin')->find($params['id']);
                uploadDeleteFile([[$oldImg->img,$oldImg->img_origin]]);
            }
            $model = new ShopInfo();
            if($model->allowField(true)->isUpdate(true)->save($params)){
                return ['error'=>0,'msg'=>'修改店铺信息成功!'];
            }else{
                return ['error'=>1,'msg'=>'修改店铺信息失败!'];
            }
        }
        $this->assign([
            'info' => ShopInfo::find(),
        ]);
        return $this->fetch();
    }
//        if($this->request->isPost()){
//            $data = input('post.');
//            $imgs = explode(',',$data['imgs']);
//            $qiniu = new \QiniuUpload\QiNiuUpload();
//
//            foreach ($imgs as $v){
//                if(strpos($v,'http://p8jp8dv93.bkt.clouddn.com')){
//                    continue;
//                }
//                $data['content'] = str_replace(substr($v,10,62),$qiniu->localUpload(substr($v,27,45)),$data['content']);
//                uploadDelete([substr($v,27,45)],0);
//            }
//
//            $shopinfo = new ShopInfo();
//            $shopinfo->id = 1;
//            $shopinfo->name = $data['name'];
//            $shopinfo->notice = $data['notice'];
//            $shopinfo->address = $data['address'];
//            $shopinfo->job_time = $data['job_time'];
//            $shopinfo->phone = $data['phone'];
//            $shopinfo->content = $data['content'];
//            if(isset($data['img'])){
//                $shopinfo->img = $data['img'][0];
//            }
//            $shopinfo->isUpdate(true)->save();
//            return array('msg' => '修改成功','status' => 1,'icon' => 1);
//        }
//        $this->assign([
//            'info' => ShopInfo::find(),
//        ]);
//        return $this->fetch();
//    }
}